package com.rmy.queue.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.rmy.CompUtils;
import com.rmy.common.core.text.Convert;
import com.rmy.common.utils.DateUtils;
import com.rmy.domain.CompArea;
import com.rmy.queue.common.QueueConst;
import com.rmy.queue.common.webSocket.CompShowWebSocketServer;
import com.rmy.queue.domain.QmsCompConfig;
import com.rmy.queue.service.IQmsCompConfigService;
import com.rmy.queue.service.IQmsMainBusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rmy.queue.mapper.QmsNoticePublishMapper;
import com.rmy.queue.domain.QmsNoticePublish;
import com.rmy.queue.service.IQmsNoticePublishService;
import org.springframework.util.Assert;

/**
 * 通知公告Service业务层处理
 *
 * @author rmy
 * @date 2022-11-10
 */
@Service
public class QmsNoticePublishServiceImpl implements IQmsNoticePublishService
{
    @Autowired
    private QmsNoticePublishMapper qmsNoticePublishMapper;

    @Autowired
    private IQmsCompConfigService qmsCompConfigService;

    @Autowired
    private IQmsMainBusinessService qmsMainBusinessService;

    /**
     * 查询通知公告
     *
     * @param id 通知公告主键
     * @return 通知公告
     */
    @Override
    public QmsNoticePublish selectQmsNoticePublishById(Long id)
    {
        return qmsNoticePublishMapper.selectQmsNoticePublishById(id);
    }

    /**
     * 查询通知公告列表
     *
     * @param qmsNoticePublish 通知公告
     * @return 通知公告
     */
    @Override
    public List<QmsNoticePublish> selectQmsNoticePublishList(QmsNoticePublish qmsNoticePublish)
    {
        return qmsNoticePublishMapper.selectQmsNoticePublishList(qmsNoticePublish);
    }

    /**
     * 新增通知公告
     *
     * @param qmsNoticePublish 通知公告
     * @return 结果
     */
    @Override
    public int insertQmsNoticePublish(QmsNoticePublish qmsNoticePublish)
    {
        qmsNoticePublish.setCreateTime(DateUtils.getNowDate());
        return qmsNoticePublishMapper.insertQmsNoticePublish(qmsNoticePublish);
    }

    /**
     * 修改通知公告
     *
     * @param qmsNoticePublish 通知公告
     * @return 结果
     */
    @Override
    public int updateQmsNoticePublish(QmsNoticePublish qmsNoticePublish)
    {
        qmsNoticePublish.setUpdateTime(DateUtils.getNowDate());
        return qmsNoticePublishMapper.updateQmsNoticePublish(qmsNoticePublish);
    }

    /**
     * 批量删除通知公告
     *
     * @param ids 需要删除的通知公告主键
     * @return 结果
     */
    @Override
    public int deleteQmsNoticePublishByIds(Long[] ids)
    {
        return qmsNoticePublishMapper.deleteQmsNoticePublishByIds(ids);
    }

    /**
     * 删除通知公告信息
     *
     * @param id 通知公告主键
     * @return 结果
     */
    @Override
    public int deleteQmsNoticePublishById(Long id)
    {
        return qmsNoticePublishMapper.deleteQmsNoticePublishById(id);
    }

    @Override
    public int publishNoticeById(QmsNoticePublish para) throws IOException {
        QmsNoticePublish qmsNoticePublish = qmsNoticePublishMapper.selectQmsNoticePublishById(para.getId());
        Assert.notNull(qmsNoticePublish, "发布通知公告不存在，请刷新页面再操作！");
        Assert.hasText(para.getNoticeTypes(), "通知渠道必须勾选！");

        // 判断通知类型
        String[] arrNoticeType = para.getNoticeTypes().split(",");
        for (String strType : arrNoticeType) {
            switch (strType) {
                case "1": // 综合屏
                    sendToComp(qmsNoticePublish);
                    break;
                case "2": // 短信

                    break;
                default:

                    break;
            }
        }
        return 1;
    }

    public void sendToComp(QmsNoticePublish qmsNoticePublish) throws IOException {
        QmsCompConfig qmsCompConfig = new QmsCompConfig();
        qmsCompConfig.setStatus("0");
        List<String> orgList = new ArrayList<>();
        List<QmsCompConfig> compConfigs = qmsCompConfigService.selectQmsCompConfigList(qmsCompConfig);
        for (QmsCompConfig qmsComp : compConfigs) {
            switch (qmsComp.getCompType()) {
                case "1": // LED
                    String strLEDJson = qmsComp.getContentTemplate();
                    List<CompArea> newAreaList = new ArrayList<>();
                    List<CompArea> compAreaList = CompUtils.parseContent(strLEDJson);
                    for (CompArea compArea : compAreaList) {
                        if ("2".equals(compArea.getType())) {
                            compArea.setContent(qmsNoticePublish.getNoticeContent());
                        }
                        newAreaList.add(compArea);
                    }
                    qmsComp.setContentTemplate("{\"areas\":" + JSON.toJSONString(newAreaList) + "}");
                    qmsCompConfigService.updateQmsCompConfig(qmsComp);
                    boolean bSent = false;
                    for (String orgNo : orgList) {
                        if (orgNo.equals(qmsComp.getOrgNo())) {
                            bSent = true;
                            break;
                        }
                    }
                    if (!bSent) {
                        orgList.add(qmsComp.getOrgNo());
                        qmsMainBusinessService.sendCompScreen(qmsComp.getOrgNo());
                    }
                    break;
                case "2": // LCD
                    String strJson = qmsComp.getContentTemplate();
                    JSONObject jObj = JSONObject.parse(strJson);
                    jObj.put("foot_content", qmsNoticePublish.getNoticeContent());
                    qmsComp.setContentTemplate(jObj.toString());
                    qmsCompConfigService.updateQmsCompConfig(qmsComp);

                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("cmd", QueueConst.WEBSOCKET_CMD.CMD_COMP_PUB_NOTICE);
                    CompShowWebSocketServer.sendMessage(qmsComp.getOrgNo(), Convert.toStr(qmsComp.getId(), ""), jsonObject.toString());
                    break;
                default:

                    break;
            }
        }
    }
}
